package behaviour.interpreter;

/**
 * 解释器模式
 * 如果一种特定类型的问题发生的频率足够高，那么可能就值得将该问题的各个实例表述为一个简单语言中的句子。
 * 这样就可以构建一个解释器，该解释器通过解释这些句子来解决该问题。
 *
 * @author yeyangtao created at 2020/7/7 09:53
 */
public class Demo {
    public static Expression getMaleExpression() {
        Expression robert = new TerminalExpression("Robert");
        Expression john = new TerminalExpression("john");
        return new OrExpression(robert, john);
    }

    public static Expression getMarriedExpression() {
        Expression julie = new TerminalExpression("julie");
        Expression married = new TerminalExpression("Married");
        return new OrExpression(julie, married);
    }

    public static void main(String[] args) {
        Expression isMale = getMaleExpression();
        Expression isMarried = getMarriedExpression();

        System.out.println("John is male? " + isMale.interpreter("John"));
        System.out.println("Julie is a married women? "
                + isMarried.interpreter("Married Julie"));
    }
}
